Comparing Text


wcscmp

The function wcscmp is used to compare two variables of the type wchar_t. La function wcscmp returns zero when both text strings are equal. If the first text string is lexicographically bigger (in the order in the dictionary) that the second one, then the function returns a positive number; if the first text string is smaller than the second one, the function returns a negative number. If a variable of the type wstring is used instead, the operators ==, <, >, etc. can be used to compare text.
La función wcscmp es usada para comparar dos variables del tipo wchar_t. La función wcscmp regresa cero cuando ambas cadenas de texto son iguales. Si la primer cádena de texto es mayor lexicograficamente (orden en el diccionario) que la segunda, entonces se regresa un numero positivo; si la primer cadena de texto es menor que la segunda, la función regresa un numero negativo. Si una variable del tipo wstring es usada en su lugar, los operadores de ==, <, >, etc, permiten comparar texto.

Problem 1
Create a program called Comparador to guess a secret word. The user types a secret word presses the button to check the word. Suppose that the secret word is Nancy.
Cree un programa llamado Comparador para adivinar una palabra secreta. El usuario escribe una palabra secreta y presionar el botón para verificar la palabra. Suponga que la palabra secreta es Nancy.

Comparador.cpp
void Comparador::Window_Open(Win::Event& e)
{
     this->radioWchar_t.Checked = true;
}

void Comparador::btCheck_Click(Win::Event& e)
{
     if (radioWstring.Checked == true)
     {
          //________________________________________ Using wstring
          if (tbxSecretWord.Text == L"Nancy")
          {
               tbxSecretWord.ShowBalloonTip(L"Correct", L"Comparador", TTI_INFO);
          }
          else
          {
               tbxSecretWord.ShowBalloonTip(L"Incorrect", L"Comparador", TTI_ERROR);
          }
     }
     else
     {
          //________________________________________ Using wchar_t
          if (wcscmp(tbxSecretWord.Text.c_str(), L"Nancy") == 0)
          {
               tbxSecretWord.ShowBalloonTip(L"Correct", L"Comparador", TTI_INFO);
          }
          else
          {
               tbxSecretWord.ShowBalloonTip(L"Incorrect", L"Comparador", TTI_ERROR);
          }
     }
}

Comparador1

Comparador2

Problem 2
Create a program called Access as shown below. The program checks whether the word Cerveza is included in the sentence or not. If the word is included, the window title changes to "Que pasa?"; if the word is not included, the window title must change to "Error de acceso". As it can be seen, the program does not have a button, and the sentence should be verified as the user types the text in the textbox. Double click in the textbox and check the event "Change" as shown below.
Cree un programa llamado Access como se muestra debajo. Cuando el usuario presiona el botón, el programa checa si la palabra Cerveza está incluida en la frase o no. Si la palabra está incluida, el título de la ventana cambia a "Que pasa?"; si la palabra no está incluida, el título de la ventana deberá ser "Error de acceso". Como puede verse, el programa no tiene un botón, y la frase se tiene que verificar mientras el usuario escribe en la caja de texto. Haga doble clic en la caja de texto y marque el evento "Change" como se muestra debajo.

Access1

Access2

AccessTextChangeEvent

Access.cpp
void Access::Window_Open(Win::Event& e)
{
}

void Access::tbxInput_Change(Win::Event& e)
{
     size_t position = tbxInput.Text.find(L"Cerveza");
     if (position == std::wstring::npos)
     {
          this->Text = L"Error!!!";
     }
     else
     {
          this->Text = L"Que Pasa?";
     }
}

Tip
In the previous example, the function (the method) find of the text variable returns the position of the word Cerveza if found; otherwise it returns a value of npos as shown. The code show below how to find a text string inside another one using wchar_t instead of wstring.
En el ejemplo previo, la función (el método) find de la variable de texto regresa la posición de la palabra Cerveza si se encuentra; de otra forma ésta regresa un valor de npos como se muestra. El código mostrado debajo muestra cómo encontrar una cadena de texto dentro de otra usando wchar_t en lugar de wstring.

Access.cpp
void Access::Window_Open(Win::Event& e)
{
}

void Access::tbxInput_Change(Win::Event& e)
{
     wchar_t text[64];
     tbxInput.GetWindowText(text, 64);
     if (wcsstr(text, L"Cerveza") == NULL)
     {
          this->Text = L"Error!!!";
     }
     else
     {
          this->Text = L"Que Pasa?";
     }
}

Tip
Wintempla provides several functions to manipulate text as shown in Class View in the figure below. You can use these functions in your code or you can learn from these functions to create new ones. The following steps allow you to explore these functions.
  1. Open Class View
  2. Open the namespace Sys
  3. Select TextAssistant to see the list of functions

Wintempla proporciona varias funciones para manipular texto como se muestra en la vista de Clases de la figura de abajo. Usted puede usar estas funciones en su código o aprender de estas funciones para crear nuevas funciones. Los siguientes pasos le permite explorar estas funciones.
  1. Abra la vista de Clases
  2. Abra el namespace Sys
  3. Seleccione TextAssistant para ver la lista de funciones

TextAssistant

wcsstr

wcsstr is very similar to the function (method) find of wstring. It allows finding a specific text string inside another text string. wcsstr returns the position of the first occurrence of the string or NULL if the string is not found.
wcsstr is muy similar a la función (método) find de wstring. Este permite encontrar una cadena de texto específica dentro de otra cadena de texto. wcsstr regresa la posición de la primera ocurrencia de la cadena o NULL si la cadena no se encuentra.

Problem 3
Compute the output of the program.
Calcule la salida del programa.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     const wchar_t* p = wcsstr(L"Excellents Programs", L"Pro");
     if (p != NULL) tbx1.Text += L"Pro was found\r\n";

     p = wcsstr(L"Excellents Programs", L"Hola");

     if (p != NULL)
     {
          tbx1.Text += L"Hola was found";
     }
     else
     {
          tbx1.Text += L"Hola was NOT found";
     }
}

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home